package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;


@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;



    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin,LocalDateTime end) {

         //  1.获得新用户数
       Integer newUsers = userMapper.getNewUsers(begin, end);

        //  2.获得订单完成率
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        Integer totalOrderCount = orderMapper.getOrderByMap(map);
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.getOrderByMap(map);
        Double orderCompletionRate =validOrderCount * 1.0 / totalOrderCount;

        //  3.获得营业额
        Double turnover = orderMapper.getTurnoverByMap(map);

        //  4.平均客单价
        Double unitPrice;
        if(validOrderCount == 0){
            unitPrice = 0.0;
        }else{
            unitPrice = turnover / validOrderCount;
        }

        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .newUsers(newUsers)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
               .build();



        return  businessDataVO;
    }

    @Override
    public SetmealOverViewVO getSetmealOverview() {
        // 1.已启售套餐数量
        Integer sold = setmealMapper.getSetmealCountByStatus(1);
        // 2.已停售套餐数量
        Integer discontinued = setmealMapper.getSetmealCountByStatus(0);

        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return setmealOverViewVO;
    }

    @Override
    public DishOverViewVO getDishOverview() {
        // 1.已启售菜品数量
        Integer sold = dishMapper.getDishCountByStatus(1);
        // 2.已停售菜品数量
        Integer discontinued = dishMapper.getDishCountByStatus(0);

        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return  dishOverViewVO;
    }

    @Override
    public OrderOverViewVO getOrderCount() {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);

        //待接单
        Integer waitingOrders = orderMapper.countByMap(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }
}
